/*
 * Copyright (c) 2016,Deepspring Inc. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This product is protected by copyright and distributed under
 * licenses restricting copying, distribution, and decompilation.
 */

package com.rs.lc.pm.utils;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoader;

public class SpringBeanUtil {

    private static ApplicationContext applicationContext;

    public static <T> T getBean(Class<T> clazz, String beanName) {
        ApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
        if (context == null) {
            context = applicationContext;
        }
        T t = context.getBean(clazz);
        if (t != null) {
            return t;
        }
        return (T) context.getBean(beanName);
    }

    public static void setApplicationContext(ApplicationContext applicationContext) {
        SpringBeanUtil.applicationContext = applicationContext;
    }
}
